/****************************************************************************
**
** Copyright (C) 2013 Ernesto Barragan <ernestus at gmail dot com>
** Based on code by the Qt project:
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/

#include "console.h"

#include <QScrollBar>

#include <QtCore/QDebug>

Console::Console(QWidget *parent)
    : QPlainTextEdit(parent),
    m_localEchoEnabled(false),
    m_hexModeEnabled(false)
{
    document()->setMaximumBlockCount(100);
    QPalette p = palette();
    p.setColor(QPalette::Base, Qt::black);
    p.setColor(QPalette::Text, Qt::green);
    setPalette(p);
}

void Console::putData(const QByteArray &data)
{
    if( m_hexModeEnabled )
    {
        /*@todo:
          show alternative lines, the upper line in HEX, the lower in Latin1
          or whatever local page the user has set.
        */
        foreach( const quint8 byte, data )
        {
            /*
                An example of data, in hex mode, 0x2B, '+' in Latin1, is shown as 2B+ with an extra space
                added at the end.
             */
            insertPlainText( QString("%1%2 ").arg( byte, 2, 16, QLatin1Char( '0' ) ).arg( QLatin1Char( byte ) ) );
        }
        //insertPlainText(QString(data.toHex())) ;
    }
    else
    {
        insertPlainText(QString(data)) ;
    }

    QScrollBar *bar = verticalScrollBar() ;
    bar->setValue(bar->maximum()) ;
}

void Console::displayDataFromPort(const QByteArray &data)
{
    //@fixme: set color to port color, not implemented yet
    QPalette p = palette();
    p.setColor(QPalette::Text, Qt::green);
    setPalette(p);
    putData( data ) ;
}

void Console::displayDataFromUi(const QByteArray &data)
{
    if( m_localEchoEnabled )
    {
        //@fixme: set color to Ui color, not implemented yet
        QPalette p = palette();
        p.setColor(QPalette::Text, Qt::yellow);
        setPalette(p);
        putData( data ) ;
    }
    emit getData( data );
}

void Console::setLocalEchoEnabled(bool set)
{
    m_localEchoEnabled = set;
}

void Console::keyPressEvent(QKeyEvent *e)
{
    switch (e->key())
    {
    /*
    case Qt::Key_Backspace:
    case Qt::Key_Left:
    case Qt::Key_Right:
    case Qt::Key_Up:
    case Qt::Key_Down:
        // skip processing
        break;
        */
    default:
        if (m_localEchoEnabled)
        {
            //QPlainTextEdit::keyPressEvent( e );
            QByteArray ba ;
            ba.append( e->text().toLatin1() ) ;
            putData( ba );
        }
        emit getData( e->text().toLatin1() );
    }
}

void Console::mousePressEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
    setFocus();
}

void Console::mouseDoubleClickEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
    setFocus();
}

void Console::contextMenuEvent(QContextMenuEvent *e)
{
    Q_UNUSED(e)
    setFocus();
}

bool Console::hexModeEnabled() const
{
    return m_hexModeEnabled;
}

void Console::setHexModeEnabled(bool hexModeEnabled)
{
    m_hexModeEnabled = hexModeEnabled;
}

